﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLServerDAL
{
    public class Attachment : BaseDAL, IAttachment
    {

        public int Insert(Models.Attachment model)
        {
            if (model.ID <= 0)
                model.ID = Db.CreateKey("Attachment");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,50),
					new SqlParameter("@OriginalName", SqlDbType.VarChar,200),
					new SqlParameter("@SaveName", SqlDbType.VarChar,200),
					//new SqlParameter("@Guid", SqlDbType.VarChar,32),
					new SqlParameter("@Size", SqlDbType.Int,8),
					new SqlParameter("@IsPicture", SqlDbType.Bit,1),
					new SqlParameter("@Extension", SqlDbType.VarChar,50),
					new SqlParameter("@HasThumbnails", SqlDbType.Bit,1),
					new SqlParameter("@ThumbnailName", SqlDbType.VarChar,200),
					new SqlParameter("@Mime", SqlDbType.VarChar,200),
					new SqlParameter("@CreateTime", DbType.DateTime),
					new SqlParameter("@Folder", SqlDbType.VarChar,200),
					new SqlParameter("@ValueID", SqlDbType.Int,8),
					new SqlParameter("@TypeID", SqlDbType.Int,8),
                    new SqlParameter("@UserID", SqlDbType.Int,8),
					new SqlParameter("@ID", SqlDbType.Int,8)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.OriginalName;
            parameters[2].Value = model.SaveName;
            //parameters[3].Value = model.Guid;
            parameters[3].Value = model.Size;
            parameters[4].Value = model.IsPicture;
            parameters[5].Value = model.Extension;
            parameters[6].Value = model.HasThumbnails;
            parameters[7].Value = model.ThumbnailName;
            parameters[8].Value = model.Mime;
            parameters[9].Value = model.CreateTime;
            parameters[10].Value = model.Folder;
            parameters[11].Value = model.ValueID;
            parameters[12].Value = model.TypeID;
            parameters[13].Value = model.UserID;
            parameters[14].Value = model.ID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Attachment([Title],[OriginalName],[SaveName],[Size],[IsPicture],[Extension],[HasThumbnails],[ThumbnailName],[Mime],[CreateTime],[Folder],[ValueID],[TypeID],[UserID],[ID]) values (@Title,@OriginalName,@SaveName,@Size,@IsPicture,@Extension,@HasThumbnails,@ThumbnailName,@Mime,@CreateTime,@Folder,@ValueID,@TypeID,@UserID,@ID)", parameters);
        }

        public int Update(int value, int typeId, string[] ids)
        {
            if (ids == null || ids.Length == 0)
                return 0;
            if (ids.Length == 1)
                return Db.Helper.ExecuteNonQuery(string.Concat("update JNCms_Attachment set [ValueID]=", value.ToString(), ",[TypeID]=", typeId.ToString(), " where [ID]=", ids[0]));
            return Db.Helper.ExecuteNonQuery(string.Concat("update JNCms_Attachment set [ValueID]=", value.ToString(), ",[TypeID]=", typeId.ToString(), " where [ID] in (", string.Join(",", ids), ")"));
        }

        public int Delete(int value, int typeId)
        {
            return Db.Helper.ExecuteNonQuery(string.Concat("delete from JNCms_Attachment where [ValueID]=", value.ToString(), ",[TypeID]=", typeId.ToString()));
        }

        public List<Models.Attachment> GetListByItem(int id, int typeId)
        {
            List<Models.Attachment> list = new List<Models.Attachment>();
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, string.Format("select * from JNCms_Attachment where [ValueID]={0} and [TypeID]={1} order by [CreateTime] desc", id, typeId), new SqlParameter("@ID", id)))
            {

                while (rd.Read())
                {
                    list.Add(Db.Entity.PopulateEntity<Models.Attachment>(rd));
                }
                //list = Db.Entity.PopulateEntityCollection<Models.Attachment>(rd);
            }
            return list;
        }

        public List<Models.Attachment> GetList(int pageIndex, int pageSize)
        {

            List<Models.Attachment> list = new List<Models.Attachment>();
            if (pageSize > 0)
            {
                string sql;
                if (pageIndex == 1)
                    sql = pageSize.ToString();
                else
                    sql = string.Concat(Convert.ToString(pageSize * (pageIndex - 1)), ",", Convert.ToString(pageSize * pageIndex));

                using (IDataReader rd = Db.Helper.ExecuteReader(string.Concat("select * from [JNCms_Attachment] order by [CreateTime] desc limit ", sql)))
                {
                    while (rd.Read())
                    {
                        list.Add(Db.Entity.PopulateEntity<Models.Attachment>(rd));
                    }
                }

            }
            return list;
        }

        public Models.Attachment GetItem(int id)
        {
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from [JNCms_Attachment] where [ID]=@ID limit 1", new SqlParameter("@ID", id)))
            {
                if (rd.Read())
                {
                    return Db.Entity.PopulateEntity<Models.Attachment>(rd);
                }
            }
            return null;
        }

        public int Delete(int id)
        {
            return Db.Helper.ExecuteNonQuery(string.Concat("delete from JNCms_Attachment where [ID]=", id.ToString()));
        }
    }
}
